/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.ugent.medirem.logic;

import be.ugent.medirem.persistence.entity.details.ConsultationDetails;
import be.ugent.medirem.persistence.entity.details.GpDetails;
import be.ugent.medirem.persistence.entity.details.LocationDetails;
import be.ugent.medirem.persistence.entity.details.PatientDetails;
import be.ugent.medirem.persistence.entity.values.LocationValues;
import be.ugent.medirem.persistence.entity.values.PatientValues;
import be.ugent.medirem.persistence.exception.DaoException;
import be.ugent.medirem.persistence.exception.EntityNotFoundException;
import be.ugent.medirem.persistence.facade.ConsultationDaoLocal;
import be.ugent.medirem.persistence.facade.LocationDaoLocal;
import be.ugent.medirem.persistence.facade.PatientDaoLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author Thomas Roelens
 */
@Stateless
public class PatientBean implements PatientRemote {

    @EJB
    private ConsultationDaoLocal consultationDaoBean;
    @EJB
    private LocationDaoLocal locationDaoBean;
    @EJB
    private PatientDaoLocal patientDaoBean;

    public Long createPatient(PatientValues values) {
        return patientDaoBean.create(values);
    }

    public Long createPatient(String username, String password, String socialsecuritynr) {
        return patientDaoBean.create(username, password, socialsecuritynr);
    }

    public PatientDetails getPatientDetails(Long patientId) throws EntityNotFoundException, IllegalArgumentException {
        return patientDaoBean.getDetails(patientId);
    }

    public LocationDetails getLocationDetails(Long patientId) throws EntityNotFoundException, IllegalArgumentException {
        return patientDaoBean.getLocationDetails(patientId);
    }

    public List<PatientDetails> getAllPatients() {
        return patientDaoBean.getAllPatients();
    }

    public void removePatient(Long patientId) throws EntityNotFoundException, IllegalArgumentException {
        patientDaoBean.remove(patientId);
    }

    public void updatePatient(PatientValues details) throws EntityNotFoundException, IllegalArgumentException {
        patientDaoBean.update(details);
    }

    public void updatePatient(Long patientId, Long locationIdToUpdate, LocationValues locationDetails) throws EntityNotFoundException, IllegalArgumentException {
        Long locationId = null;
        // Create a new Location
        if (locationIdToUpdate == null) {
            try {
                locationId = locationDaoBean.create(locationDetails);
            } catch (DaoException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            try {
                locationDaoBean.update(locationIdToUpdate, locationDetails);
                locationId = locationIdToUpdate;
            } catch (EntityNotFoundException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            } catch (DaoException ex) {
                Logger.getLogger(PatientBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        patientDaoBean.update(patientId, locationId);
    }

    public void updatePatientPassword(Long id, String oldPassword, String newPassword) throws EntityNotFoundException, IllegalArgumentException {
        patientDaoBean.updatePassword(id, oldPassword, newPassword);
    }

    public List<GpDetails> getGpPublicDetails(Long patientId) {
        return patientDaoBean.getGpPublicDetails(patientId);
    }

    public Long getPatientId(String username) throws IllegalArgumentException {
        return patientDaoBean.getPatientId(username);
    }

    public PatientDetails searchPatientsBySocialSecurityNumber(String number) {
        return patientDaoBean.searchPatientsBySocialSecurityNumber(number);
    }

    public List<ConsultationDetails> getConsultations(Long id) {
        return consultationDaoBean.getPatientConsultations(id);
    }
}
